import java.util.*;

public class Solution1001 {

    int[] dx={-1,-1,-1,0,0,0,1,1,1};
    int[] dy={-1,0,1,-1,0,1,-1,0,1};

    public int[] gridIllumination(int n, int[][] lamps, int[][] queries) {
        Map<Integer,Integer> hang=new HashMap<>();
        Map<Integer,Integer> lie=new HashMap<>();
        Map<Integer,Integer> leftUp=new HashMap<>();
        Map<Integer,Integer> leftDown=new HashMap<>();
        Map<List<Integer>,Boolean> on=new HashMap<>();
        for (int[] lamp : lamps) {
            List<Integer> list=new ArrayList<>();
            list.add(lamp[0]);
            list.add(lamp[1]);
            if (!on.containsKey(list)) {
                hang.put(lamp[0],hang.getOrDefault(lamp[0],0)+1);
                lie.put(lamp[1],lie.getOrDefault(lamp[1],0)+1);
                leftUp.put(lamp[0]+lamp[1],leftUp.getOrDefault(lamp[0]+lamp[1],0)+1);
                leftDown.put(lamp[0]-lamp[1],leftDown.getOrDefault(lamp[0]-lamp[1],0)+1);
                on.put(list,true);
            }
        }
        int[] ans=new int[queries.length];
        for (int i=0;i<queries.length;i++){
            int[] query=queries[i];
            if (hang.getOrDefault(query[0],0)>0 || lie.getOrDefault(query[1],0)>0 || leftUp.getOrDefault(query[0]+query[1],0)>0 || leftDown.getOrDefault(query[0]-query[1],0)>0){
                ans[i]=1;
            }else{
                ans[i]=0;
            }
            for (int j=0;j<9;j++){
                int x=query[0]+dx[j];
                int y=query[1]+dy[j];
                List<Integer> list=new ArrayList<>();
                list.add(x);
                list.add(y);
                if (on.containsKey(list) && on.get(list)){
                    on.put(list,false);
                    hang.put(x,hang.get(x)-1);
                    lie.put(y,lie.get(y)-1);
                    leftUp.put(x+y,leftUp.get(x+y)-1);
                    leftDown.put(x-y,leftDown.get(x-y)-1);
                }
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(new Solution1001().gridIllumination(6,new int[][]{{2,5},{4,2},{0,3},{0,5},{1,4},{4,2},{3,3},{1,0}},new int[][]{{4,3},{3,1}})));
    }
}
